#!/usr/bin/env python
# coding:utf-8
'''
DMZAvatar统计
'''
import os,sys,datetime,common.db
from logging import *
from common.date_time import *

def get_music_sql_condition():
    # 获取xml
    from urllib import urlopen
    try:
        file_xml=urlopen('http://114.80.90.156/quest.xml');
    except IOException:
        log('get xml failed.\n');
        return ''

    # 解析xml
    from xml.etree import ElementTree
    tree=ElementTree.parse(file_xml)
    runcond_all=tree.findall('./quest/runcond')
    result=[]
    for runcond in runcond_all:
        attribute=runcond.attrib
        if attribute.has_key('game_play') and attribute.has_key('music_id'):
            result.append('(%s,%s)'%(attribute['game_play'],attribute['music_id']))
    if len(result)>0:
        return 'where (GamePlay,MusicID) not in (%s)'%(','.join(result))
    else:
        return ''


if __name__ != "__main__":
    
    # 检验输入参数,如果参数为空,返回昨天日期
    if len(sys.argv) >= 3:
        strCurDay = sys.argv[2]
        if len(strCurDay) != 10:
            log(ERROR,'Param is error!')
            sys.exit(0)
    else:
        strCurDay = GetDay(-1) ##"2009-03-15"
    log(INFO,'StatDay : '+ strCurDay)
    
    strDay = strCurDay.replace('-','')
    
    music_sql_condition=get_music_sql_condition()

    # 连接数据库
    cnn = common.db.connect('billDB')
    cur = cnn.cursor()

    delete_sql='delete from statisticdb.DMZAvatarDay where date="%s"'%(strDay)
    cur.execute(delete_sql)
    cnn.commit()
    
    strSql = 'insert into statisticdb.DMZAvatarDay(date,zoneid,object,id,count) select %s,zoneid,"Mode",gamemode,count(*) \
               from dmz_bill_%s.game group by zoneid, gamemode'%(strDay,strDay)
    strSql1 = ('insert into statisticdb.DMZAvatarDay(date,zoneid,object,id,count) select %s,zoneid,"Music",musicid,count(*) \
               from dmz_bill_%s.game '+music_sql_condition+' group by zoneid, musicid')%(strDay,strDay)
    strSql2 = 'insert into statisticdb.DMZAvatarDay(date,zoneid,object,id,count) select %s,zoneid,"Scene",sceneid,count(*) \
               from dmz_bill_%s.game group by zoneid, sceneid'%(strDay,strDay)
    strSql3 = 'insert into statisticdb.DMZAvatarDay(date,zoneid,object,id,count) select %s,zoneid,"GamePieces",GoodsID,sum(price) \
               from dmz_bill_%s.goods_detail where paymeans = 1 group by zoneid, GoodsID'%(strDay,strDay)
    strSql4 = 'insert into statisticdb.DMZAvatarDay(date,zoneid,object,id,count) select %s,zoneid,"51Point",GoodsID,sum(price) \
               from dmz_bill_%s.goods_detail where paymeans = 2 group by zoneid, GoodsID'%(strDay,strDay)
    strSql5 = 'insert into statisticdb.DMZAvatarDay(date,zoneid,object,id,count) select %s,zoneid,"Count",GoodsID,sum(case when usemeans = 1 then 1 else quantity end) \
               from dmz_bill_%s.goods_detail group by zoneid, GoodsID'%(strDay,strDay)
    try:
        rows = cur.execute(strSql)
        rows1 = cur.execute(strSql1)
        rows2 = cur.execute(strSql2)
        rows3 = cur.execute(strSql3)
        rows4 = cur.execute(strSql4)
        rows5 = cur.execute(strSql5)
    except Exception,data:
        log(ERROR,data)
        sys.exit(0)
        
    if rows > 0 and rows1 > 0 and rows2 > 0 and rows3 > 0 and rows4 > 0 and rows5 > 0:
        log(INFO,'Data inserted successful!')
    else:
        log(INFO,'No data inserted')
    
    cnn.commit()
    cur.close()
    cnn.close()
                    
